package com.itheima.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.health.dto.CheckGroupDto;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "检查组模块管理")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    @PostMapping("checkgroup/add")
    @ApiOperation(value = "添加检查组方法",notes = "添加检查组信息")
    public Result add(@RequestBody CheckGroupDto checkGroupDto){
        try{
            checkGroupService.saveOrUpdateCheckGroup(checkGroupDto);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @PostMapping("checkgroup/findPage")
    @ApiOperation(value = "查询检查组方法",notes = "分页查询检查组的信息")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = checkGroupService.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @GetMapping("checkgroup/findCheckItemIdsByGroupId/{id}")
    @ApiOperation(value = "查询检查组中检查项ID集合的方法",notes = "查询检查组中检查项ID集合")
    public Result findCheckItemIdsByGroupId(@PathVariable("id") int id){
        try {
            Map map = checkGroupService.findCheckItemIdsByGroupId(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @DeleteMapping("checkgroup/deleteCheckGroupById")
    @ApiOperation(value = "删除检查组的方法",notes = "根据ID删除对应的检查组")
    public Result deleteCheckGroupById(@RequestParam("id") Integer id){
        try {
            //逻辑删除
            CheckGroup checkGroup = new CheckGroup();
            checkGroup.setId(id);
            checkGroup.setIs_delete(1);
            checkGroupService.updateById(checkGroup);
            //物理删除
//            checkGroupService.deleteChenckGroupAndCheckItemById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    @GetMapping("checkgroup/findAll")
    @ApiOperation(value = "查询所有检查组方法",notes = "无条件查询所有检查组")
    public Result findAll(){
        try {
            List<CheckGroup> list = checkGroupService.list();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
